package handel

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"role-perm-mgr/cache"
	"role-perm-mgr/model"
	"role-perm-mgr/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	redis_key := "user_login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误大于三次禁止登录",
		})
		return
	}
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	var user model.User
	err := model.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次封禁两个小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func UserRole(c *gin.Context) {
	user_id, _ := strconv.Atoi(c.PostForm("user_id"))
	role_id, _ := strconv.Atoi(c.PostForm("role_id"))
	data := model.UserRole{
		UserId: user_id,
		RoleId: role_id,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func RoleNode(c *gin.Context) {

	role_id, _ := strconv.Atoi(c.PostForm("role_id"))
	node_id, _ := strconv.Atoi(c.PostForm("node_id"))
	data := model.RoleNode{
		RoleId: role_id,
		NodeId: node_id,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func UserRoleFind(c *gin.Context) {
	/*
			SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.role_id = roles.id
		WHERE
		  user_roles.user_id = 1
	*/
	var role model.Role
	id := c.Query("id")
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.role_id = roles.id").
		Where(" user_roles.user_id = ?", id).
		First(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
}
func RoleNodeFind(c *gin.Context) {
	/*
			SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.node_id=nodes.id
		WHERE
		  role_nodes.role_id=1
	*/
	var node model.Node
	id := c.Query("id")
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.node_id=nodes.id").
		Where(" role_nodes.role_id=?", id).
		First(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
}
func UserList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var user []model.User
	redis_key := "user_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Offset(offset).Limit(size).Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	var total int64
	model.DB.Model(&model.User{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "展示成功",
		"data":  user,
		"total": total,
	})
	return
}
func UserFind(c *gin.Context) {
	id := c.Query("id")
	var user model.User
	redis_key := "user_find_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": user,
	})
	return
}
